package com.whut.gkd.controller;


import com.whut.gkd.bean.PageInfo;
import com.whut.gkd.bean.ShopInfo;
import com.whut.gkd.bean.User;
import com.whut.gkd.service.IGoodService;
import com.whut.gkd.service.IShopService;
import com.whut.gkd.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    IGoodService goodService;

    @Autowired
    IShopService shopService;
    @RequestMapping("login.do")
    public ModelAndView login(User user, HttpSession session){
        boolean flag = userService.login(user.getUsername(),user.getPassword());
        ModelAndView modelAndView = new ModelAndView();
        if(flag){
            User users = userService.findUserByName(user.getUsername());
            session.setAttribute("user",users);
            if(users.getStatus().equalsIgnoreCase("normal")){
                if(users.getRole().equalsIgnoreCase("trader")){
                    ShopInfo shopInfo = shopService.findShopinfoByUserId(users.getId());
                    session.setAttribute("shopinfo",shopInfo);
                }
                modelAndView.setViewName("main");
            }else{
                modelAndView.setViewName("../suspended");
            }
        }else{
            modelAndView.setViewName("../failer");
        }
        return modelAndView;
    }



    @RequestMapping("findAll.do")
    public ModelAndView findAll(@RequestParam(defaultValue = "1") int currentPage, String username,
                                @RequestParam(defaultValue = "0") int type ,
                                HttpSession session) {
        if(type == 1){
            session.setAttribute("searchName",username);
        }else if(type == 0){
            username = (String) session.getAttribute("searchName");
        }else if(type == 2){
            session.removeAttribute("searchName");
        }

        ModelAndView mv = new ModelAndView();
        PageInfo pageInfo = userService.findAll(currentPage,username);
        mv.setViewName("user-list");
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }

    @RequestMapping("deleteById.do")
    public String delete(int id){
       userService.deleteById(id);
       return "redirect:findAll.do";
    }


    @RequestMapping("add.do")
    public String add(User user){
        userService.add(user);
        return "redirect:findAll.do";
    }

    @RequestMapping("toUpdate.do")
    public ModelAndView toUpadte(int id){
        User user = userService.selectUserById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("user",user);
        mv.setViewName("user-update");
        return mv;
    }

    @RequestMapping("update.do")
    public String update(User user){
        userService.update(user);
        return "redirect:showUserInfo.do";
    }


    @RequestMapping("logout.do")
    public String logout(HttpSession session)
    {
        session.removeAttribute("user");
        return "../login";
    }


    @RequestMapping("showUserInfo")
    public ModelAndView showUserInfo(User user,HttpSession session)
    {
        ModelAndView modelAndView = new ModelAndView();
        user = (User)session.getAttribute("user");
        modelAndView.addObject("user",user);
        modelAndView.setViewName("user-info");
        return modelAndView;
    }

//    @RequestMapping("register.do")
//    public String register(User user)
//    {
//        String username = user.getUsername();
//        String password = user.getPassword();
//        userService.addUser(user);
//        return "../login";
//    }

    @RequestMapping("userRegister.do")
    public String userRegister(User user){
        userService.addUser(user);
        return "../login";
    }


    @RequestMapping("toApplyRole.do")
    public ModelAndView toApplyRole(User user,HttpSession session)
    {
        ModelAndView modelAndView = new ModelAndView();
        user = (User)session.getAttribute("user");
        modelAndView.addObject("user",user);
        modelAndView.setViewName("user-role");
        return modelAndView;
    }

    @RequestMapping("applyManager.do")
    public String applyAdmin(int applicanId,String applicanName,String reason){

        userService.applyManager(applicanId,applicanName,reason);
        return "redirect:showUserInfo.do";

    }

    @RequestMapping("applyShop.do")
    public String applyShop(int applicanId,String applicanName,String reason,String shopName,String shopDescription){

        userService.applyShop(applicanId,applicanName,reason,shopName,shopDescription);
        return "redirect:showUserInfo.do";

    }

    @PostMapping("deleteAll.do")
    @ResponseBody
    public String deleteAll(String userList){
        String[] strs = userList.split(",");
        List<Integer> ids = new ArrayList<>();
        for(String s:strs){
            ids.add(Integer.parseInt(s));
        }
        userService.deleteAll(ids);
        return "";
    }


    @RequestMapping("suspendedBYId.do")
    public String suspendedBYId(int id) {
        userService.suspendedBYId(id);

        return  "redirect:findAll.do";
    }
    @RequestMapping("normalBYId.do")
    public String normalBYId(int id) {
        userService.normalBYId(id);

        return "redirect:findAll.do";
    }
}
